
const Leavemsg = require('../model/leavemsg.model')
const leaveComment = require('../model/leaveComment.model')
const { getLeaveComment } = require('./leaveComment.service')
class leavemsgService {
  async getLeavemsg(params){
    const json = await Leavemsg.findAll(
      {
        limit:params.pageSize,
        offset:(params.pageNo -1) * params.pageSize,
      },
    )
    for(let i = 0; i < json.length; i++){
       let res = await getLeaveComment({id:json[i].id})
       json[i].child = res
    }
    return json
  }
  async insertLeavemsg(params){
    const jane = await Leavemsg.create(params)
    if(jane){
      return true
    } else {
      return false
    }
  }
  //分页查询
  async search(params){
    let obj = Object.assign({},params)
    delete obj.pageNo
    delete obj.pageSize
    const { count, rows } = await Leavemsg.findAndCountAll(
      {
        limit:params.pageSize,
        offset:(params.pageNo -1) * params.pageSize,
        where:{...obj} 
      },
    )
    return {
      rows,
      count
    }
  }
  //删除
  async deleteOne(params){
    const res = await Leavemsg.destroy({ where:{ id:params.id } })
    return res
  }
}

module.exports = new leavemsgService()